﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ZombieMonkeys.Vibria.Logic
{
    public class PuzzleAssembler
    {
        /// <summary>
        /// Assemble a new puzzle object
        /// </summary>
        /// <param name="rows">the number of rows that the puzzle will contain</param>
        /// <param name="columns">the number columns that the puzzle will contain</param>
        /// <param name="minigrids">the number of minigrids that the puzzle will contain</param>
        /// <returns>A Puzzle for you to solve, sudoku style!</returns>
        public Puzzle Assemble(int rows, int columns, int minigrids)
        {
            int puzzleRows = rows;
            int puzzleCols = columns;
            
            
            List<Square> assembledSquares = new List<Square>();

            assembledSquares = buildSquares(puzzleRows, puzzleCols);

            Puzzle assembledPuzzle;
            //create the grid from the List<Square> squares
            Grid assembledGrid = new Grid(assembledSquares);

            //create the puzzle from the grid;
            assembledPuzzle = new Puzzle(assembledGrid);
            //create the values for the squares in the grid
            

            return assembledPuzzle;
        }
        /// <summary>
        /// build the number of squares based on the rows and cols desired
        /// </summary>
        /// <param name="rows">the number of rows for the puzzle</param>
        /// <param name="columns">the number of columns for the puzzle</param>
        /// <returns> a list of row, col assigned squares</returns>
        private List<Square> buildSquares(int rows, int columns)
        {
            Square tempSquare;
            List<Square> tempSquares = new List<Square>();

            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    tempSquare = new Square();

                    tempSquare.SetRow(i);
                    tempSquare.SetColumn(j);
                    
                    generatePuzzleValues(tempSquare);

                    tempSquares.Add(tempSquare);
                }
            }
            return tempSquares;
        }

        private void generatePuzzleValues(Square square)
        {   
            int lastNumber = 0;
            Random rand = new Random();
            //this is just to start, and we really have to go back to design if we are going to include any other types of sudoku puzzles
            //I have pretty much just been creating standard sudoku which could hurt us if we want to add in other puzzle grid sizes
            int selectedNumber = rand.Next(1, 9);
            //right now forsome reason, the numbers aren't changing from one to the next
            if (selectedNumber == lastNumber)
            {
                selectedNumber = rand.Next(1, 9);
            }
            else
            { 
                square.SetValue(selectedNumber);
            }

            lastNumber = selectedNumber;

            
            selectedNumber = 0;
        }

        private MiniGrid createMiniGrid(int rows, int cols)
        {
            MiniGrid tempMiniGrid = new MiniGrid(rows, cols);
            
            return tempMiniGrid;
        }

        
    }
}
